<?php

namespace App\Http\Controllers\Admin;
//require_once __DIR__ .'/resources/org/code/codeClass.php';        //引入扩展类
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Model\User;
//use App\Http\Requests;
//use App\Http\Controllers\Controller;

require_once dirname(__FILE__) .'/../../../../resources/org/code/codeClass.php';
//require_once __DIR__.'/resources/org/code/codeClass.php';
//use Resources\Org\Code\codeClass;

class LoginController extends CommonController
{
    public function login()
    {
        if($input=Input::all()){
           $code = new \ValidateCode();
           $_code = $code->getCode();   //获取验证码
           if(strtolower($input['code'])!=$_code){      //判断，转换为小写或者大写后判断 strtoupper strtolower
                return back()->with('msg','验证码错误');  //返回请求页面，并提示信息
           }

            $user=User::first();      //取一个数据
            //判断用户名和密码都对
            if($user->user_name!=$input['user_name'] || Crypt::decrypt($user->user_pass)!=$input['user_pass']){
                return back()->with('msg','用户名或者密码错误');
            }
            //指定session
            session(['user'=>$user]);
           // dd(session('user'));        //测试

           //登录成功
           // echo 'ok';
            //跳转到主页
           return redirect('admin/index');

           // dd($input);
        }else{
           // $user=User::all();      //测试user表
           // dd($user);
            //测试，清除session
            //session(['user'=>null]);
            return view('admin/login');
        }
       // echo 3232323;

    }

    //注销登录
    public function logout()
    {
        session(['user'=>null]);
        return redirect('admin/login');
    }

    public function code()
    {
        $code = new \ValidateCode();  //实例化一个对象
        $code->doimg();
        $_SESSION['code'] = strtolower($code->getCode());//验证码 转为小写 并保存到SESSION中
    }

    //crypt 加密【测试用】
//    public function crypt()
//    {
//        //加密
//        $str='123456';
//        $str1='xdsdsxxxx123213';
//        $str2='eyJpdiI6IlpqVUxidlRGRmlcL2gwT1dSNHVhaVVRPT0iLCJ2YWx1ZSI6IlZybmY5enQwRGN5QmV5eVBYZmVhSnozYTUrWEhMTUlDR0ZaTzc5b2drT1U9IiwibWFjIjoiMzYyYTRjMDNkNGQ3N2JmMTRjM2MxYTliYWVhZjc2NDBlZGI2Y2JkNzkwNTliNzAwYzAyN2I1OTk5NWRmZGVjMyJ9';
//        //Crypt::
//        echo Crypt::encrypt($str);
//        echo '<br>';
//        echo Crypt::encrypt($str1);
//        echo '<br>';
//        //解密
//        echo Crypt::Decrypt($str2);
//
//    }

    //仅做测试，获取验证码
    //获取验证码
//    public function getcode()
//    {
//        $code = new \ValidateCode();  //实例化一个对象
//        echo $code->getCode();
//    }




}
